﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Web;
using System.Web.Routing;

using ProjectBase.Core;
using ProjectBase.Core.Web;

namespace $safeprojectname$
{
    public static class TVCommon
    {
        /// <summary>
        ///     Creates the relative path based on the application relative path (virtual directory or not?)
        ///     useful for .css, .js etc.
        /// </summary>
        /// <param name = "path"></param>
        /// <returns></returns>
        public static string CreateUrl(string path)
        {
            return HttpContext.Current.CreateUrl(path);
        }

        /// <summary>
        ///     Disassembles quazi url strings like '/Controller/Action/stuff',
        ///     'Controller', 'Controller.mvc/Action/id=1' and recreates the correct Url.Action
        ///     based on Global.asax settings
        /// </summary>
        /// <param name = "target"></param>
        /// <param name = "urlHelper"></param>
        /// <param name = "routeValues"></param>
        /// <returns></returns>
        public static string CreateMvcActionUrl(string target, IUrlHelper urlHelper,
            IDictionary<string, object> routeValues)
        {
            var routeAndParams = target.Split('?');
            var parts = routeAndParams[0].Split('/');
            var start = target.StartsWith("/", StringComparison.InvariantCulture)
                            ? 1
                            : 0;
            var controller = parts[start].Split('.')[0];
            var action = parts.Length > ++start
                             ? parts[start]
                             : Constants.Actions.Default;
            var id = parts.Length > ++start
                         ? parts[start]
                         : string.Empty;

            if (routeValues.IsNull())
            {
                routeValues = new RouteValueDictionary();
            }

            if (id.IsNotEmpty())
            {
                routeValues[Const.IdAttr] = id;
            }

            if (routeAndParams.Length > 1)
            {
                foreach (var paramPair in routeAndParams[1].Split('&'))
                {
                    var pairs = paramPair.Split('=');
                    if (pairs.Length == 2)
                    {
                        routeValues[pairs[0]] = pairs[1];
                    }
                }
            }

            return urlHelper.Action(action, controller, routeValues);
        }

        #region Constants
        public static class Const
        {
            /// <summary>Contains "id"</summary>
            public const string IdAttr = "id";
            /// <summary>Contains "_checkedNodes"</summary>
            public const string SessionKeyChecked = "_checkedNodes";
            /// <summary>Contains "_expandedNodes"</summary>
            public const string SessionKeyExpanded = "_expandedNodes";

            public const string ImageNodeClosedPath = "/i/tv/xc.gif";
            public const string ImageNodeOpenedPath = "/i/tv/xo.gif";
            public const string ImageLeafPath = "/i/tv/xl.gif";

        }
        #endregion Constants
    }
}